package com.android.settings;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class WifiForWelcomeActivity extends Activity {
	private Button mBackButton;
	private Button mNextButton;
	private IntentFilter mNetworkIntentFilter;
	private BroadcastReceiver mNetworkReceiver;
	private String TAG = "WififorWelcome";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifi_for_welcome);
		setupButton();
		mNetworkIntentFilter = new IntentFilter();
		mNetworkIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		mNetworkIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		mNetworkIntentFilter
				.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
		mNetworkIntentFilter
				.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
		mNetworkIntentFilter
				.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

		mNetworkReceiver = new BroadcastReceiver() {
			private boolean DEBUG = true;

			@Override
			public void onReceive(Context context, Intent intent) {
				final String action = intent.getAction();
				if (DEBUG) {
					Log.d(TAG, "NetworkReceiver.onReceive() action=" + action);
				}
				Log.d(TAG, "NetworkReceiver.onReceive() action=" + action);
				if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
					int wifiState = intent.getIntExtra(
							WifiManager.EXTRA_WIFI_STATE,
							WifiManager.WIFI_STATE_UNKNOWN);
					handleWifiStateChanged(wifiState);
				} else {
					Log.d(TAG, "else:NetworkReceiver.onReceive() " + action);
					if (isConnected(ConnectivityManager.TYPE_WIFI)) {
						mNextButton.setEnabled(true);
					}
				}
			}

		};
	}

	private void handleWifiStateChanged(int wifiState) {
		boolean isWifiDisabled = wifiState == WifiManager.WIFI_STATE_DISABLED;
		Log.d(TAG, "NetworkReceiver.onReceive() state=" + isWifiDisabled);
		if (isConnected(ConnectivityManager.TYPE_WIFI)) {
			mNextButton.setEnabled(isWifiDisabled);
		} else {
			mNextButton.setEnabled(false);

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		unregisterReceiver(mNetworkReceiver);

		super.onPause();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		registerReceiver(mNetworkReceiver, mNetworkIntentFilter);
		super.onResume();
	}

	private void setupButton() {
		mBackButton = (Button) findViewById(R.id.back_button);
		mNextButton = (Button) findViewById(R.id.next_button);

		mBackButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mNextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {


//				Intent intent = new Intent("com.hisense.WifiConnect_Next");
//				sendStickyBroadcast(intent);
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	private boolean isConnected(int networkType) {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		if (connectivityManager == null) {
			return false;
		}

		NetworkInfo networkInfo = networkType == -1 ? connectivityManager
				.getActiveNetworkInfo() : connectivityManager
				.getNetworkInfo(networkType);
		if (networkInfo == null) {
			return false;
		}

		return networkInfo.isConnected();
	}

}
